package com.zhuiyun.project.api.topicmanage.service.Impl;

import com.zhuiyun.project.api.topicmanage.entity.Topic;
import com.zhuiyun.project.api.topicmanage.mapper.TopicMapper;
import com.zhuiyun.project.api.topicmanage.service.TopicService;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName TopicServiceImpl
 * @Description TODO
 * @Author 田
 * @Date 2023/12/11 13:58
 **/
@Service
public class TopicServiceImpl implements TopicService {

    @Resource
    private TopicMapper topicMapper;

    /**
    * @Author 田
    * @Description 话题管理查询方法
    * @Date 2023/12/13 14:30
    * @Return java.util.List<com.zhuiyun.project.api.topicmanage.entity.Topic>
    * @param topic
    */
    @Override
    public List<Topic> topicQueryAll(Topic topic) {
        List<Topic> topics = topicMapper.topicQueryAll(topic);
        return topics;
    }

    /**
    * @Author 田
    * @Description 话题管理添加方法
    * @Date 2023/12/14 9:56
    * @Return boolean
     * @param topic
     * @return
     */
    @Override
    public CommonResult topicAdd(Topic topic) {
        //判断话题名称是否为空
        if (topic.getTopicName() != null && !topic.getTopicName().equals("")) {
            boolean b = topicMapper.topicAdd(topic);
            if (b = true) {
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            } else {
                return CommonResult.error(EmErrorCode.SAVE_ERROR);
            }
        } else {
            return CommonResult.error(EmErrorCode.SAVE_ERROR);
        }
    }

    /**
    * @Author 田 
    * @Description 话题管理删除方法
    * @Date 2023/12/14 10:35
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param topic
    */
    @Override
    public CommonResult topicDelete(Topic topic) {
        if(topic.getTopicId()!=0){
            int i=topicMapper.topicDelete(topic);
            if(i>0){
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else {
                return CommonResult.error();
            }
        }else {
            return CommonResult.error(EmErrorCode.ID_NULL);
        }
    }

    /**
    * @Author 田 
    * @Description 话题管理修改方法
    * @Date 2023/12/19 8:59
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param topic
    */
    @Override
    public CommonResult topicUpdate(Topic topic) {
        if(topic.getTopicId()!=0){
            if(topic.getTopicName()!=null && topic.getTopicName()!=""){
                int update = topicMapper.topicUpdate(topic);
                if(update>0){
                    return CommonResult.ok(EmErrorCode.UPDATE_OK);
                }else{
                    return CommonResult.error(EmErrorCode.UPDATE_ERROR);
                }
            }else{
                return CommonResult.error(EmErrorCode.UPDATE_ERROR);                
            }
        }else{
            return CommonResult.error(EmErrorCode.UPDATE_ERROR);
        }
    }

    /**
    * @Author 田 
    * @Description 话题管理修改回显方法
    * @Date 2023/12/19 9:00
    * @Return com.zhuiyun.project.common.response.CommonResult
    * @param topic
    */
    @Override
    public CommonResult topicUpdateSel(Topic topic) {
        if(topic.getTopicId()!=0){
            Topic updateSel = topicMapper.topicUpdateSel(topic);
            if(updateSel!=null){
                return CommonResult.ok(updateSel);
            }else{
                return CommonResult.error(EmErrorCode.UPDATE_ERROR);
            }
        }else{
            return CommonResult.error(EmErrorCode.UPDATE_ERROR);
        }
    }

}